package com.fulong.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.fulong.common.utils.Constant;
import com.fulong.common.utils.PageHandler;
import com.fulong.entity.DeviceManageInfoEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fulong.common.utils.PageUtils;
import com.fulong.common.utils.Query;

import com.fulong.dao.DeviceManageInfoDao;

import com.fulong.service.DeviceManageInfoService;


@Service("deviceManageInfoService")
public class DeviceManageInfoServiceImpl extends ServiceImpl<DeviceManageInfoDao, DeviceManageInfoEntity> implements DeviceManageInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        //分页问题
        int currentpage  = 1 ;
        //没有页面
        if(params.get("page")!=null) {
            currentpage = Integer.parseInt(params.get("page").toString());
            params.remove("page") ;
        }
        HashMap<String , Object> map = new HashMap<>();
        map.put(Constant.PAGE , String.valueOf(currentpage)) ;
        IPage<DeviceManageInfoEntity> page = this.page(
                new Query<DeviceManageInfoEntity>().getPage(map),
                new QueryWrapper<DeviceManageInfoEntity>().allEq(params).orderByDesc("id")
        );
        return new PageUtils(page);
    }

    @Override
    synchronized public boolean alterDeviceStatus(Integer status , String id) {

        boolean update = this.update(new UpdateWrapper<DeviceManageInfoEntity>().eq("id", id).eq("status",0).set("status", status));
        return update;
    }



    @Override
    public PageUtils queryPageAgent(Map<String, Object> params) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        //分页问题
        int currentpage  = 1 ;
        //没有页面
        long count = 0L;
        boolean flag = false ;
        if(params.get("page")!=null) {
            currentpage = Integer.parseInt(params.get("page").toString());
            params.remove("page") ;
            flag = true ;
        }
        count = this.count(new QueryWrapper<DeviceManageInfoEntity>().allEq(params).orderByDesc("id"));
        PageHandler.setPage((currentpage-1)*10+" 10");
        HashMap<String , Object> map = new HashMap<>();

        map.put(Constant.PAGE , String.valueOf(currentpage)) ;
        IPage<DeviceManageInfoEntity> page = this.page(
                new Query<DeviceManageInfoEntity>().getPage(map),
                new QueryWrapper<DeviceManageInfoEntity>().allEq(params).orderByDesc("id")
        );
        page.setTotal(count);
        return new PageUtils(page.getRecords() , (int)count , (int)page.getSize() , (int)page.getCurrent());
    }

}